#ifndef link_config_widget_h
#define link_config_widget_h
#include <QWidget>
#include "typedef.h"
namespace Ui { class link_config_widget; };
#include <QMap>



class QLineEdit;

class ChannelDataWidget : public QWidget
{
	Q_OBJECT
signals:



	/// ----------------------------------------------------------------------------
	/// @brief：		发送文件单次
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_send_file_once_();

	/// ----------------------------------------------------------------------------
	/// @brief：		发送数据类型
	/// @param: 	const int send_type - send_type
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_update_send_data_type_(const int send_type);


	/// ----------------------------------------------------------------------------
	/// @brief：		发送一次数据
	/// @param: 	const QString & data - data
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_send_data_once_( const sa_def::send_data_config& config, const QString& data);


	/// ----------------------------------------------------------------------------
	/// @brief：		发送文件
	/// @param: 	bool is_sending - 是否发送文件
	/// @param: 	const QString & str_cur_file - 发送文件地址
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_send_file_(bool is_sending, const QString& str_cur_file);

	/// ----------------------------------------------------------------------------
	/// @brief：		启动定时器
	/// @param: 	const unsigned int time_interval - time_interval
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_start_send_timer_(const unsigned int time_interval);


	/// ----------------------------------------------------------------------------
	/// @brief：		停止定时器
	/// @param: 	const unsigned int time_interval - time_interval
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_stop_send_timer_();


	/// ----------------------------------------------------------------------------
	/// @brief：		清空buffer
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_clear_send_buf_();



	/// ----------------------------------------------------------------------------
	/// @brief：		刷新发送缓存, 调用前请保证数组越界检查
	/// @param: 	const QString & str_new_buf - str_new_buf
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_set_send_data_(const sa_def::send_data_config& new_data_config , const QString& str_new_buf);



	/// ----------------------------------------------------------------------------
	/// @brief：		保存文件发出的信号
	/// @param: 	const sa_def::save_to_config & config - config
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_save_to_config_(const sa_def::save_to_config& config);

	/// ----------------------------------------------------------------------------
	/// @brief：		串口接收数据配置
	/// @param: 	sa_def::sp_recv_frame_info & config - config
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void sig_sp_recv_frame_info_(sa_def::sp_recv_frame_info& config);

public:
	explicit ChannelDataWidget(QWidget *parent = Q_NULLPTR);
	~ChannelDataWidget();


	/// ----------------------------------------------------------------------------
	/// @brief：		初始化，
	/// @param: 	const sa_def::en_protocal_type & cur_channel_type - 当前是是发送串口还是网络
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_(const sa_def::enProtocalType& cur_channel_type);

	int uninit_();
	


public slots:
	/// ----------------------------------------------------------------------------
	/// @brief：		显示接收数据
	/// @param: 	const unsigned char * pbuf - pbuf
	/// @param: 	const unsigned int pbuf_len - pbuf_len
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_append_recv_data_(const unsigned char* pbuf, const unsigned int pbuf_len);

	/// ----------------------------------------------------------------------------
	/// @brief：		刷新发送计数器
	/// @param: 	const unsigned int data_len - data_len
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_update_send_counter_(const unsigned int data_len);


private slots:
	
	/// ----------------------------------------------------------------------------
	/// @brief：		发送数据类型的下拉框下拉变化
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_combo_send_data_source_cur_index_has_changed_(const QString &text);

	/// ----------------------------------------------------------------------------
	/// @brief：		选择文件按钮槽函数
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_select_file_();


	/// ----------------------------------------------------------------------------
	/// @brief：		清空发送计数器
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_clear_send_count_();

	/// ----------------------------------------------------------------------------
	/// @brief：		启动按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_start_();

	/// ----------------------------------------------------------------------------
	/// @brief：		设置按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_set_();


	/// ----------------------------------------------------------------------------
	/// @brief：		清空按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_clear_();

	/// ----------------------------------------------------------------------------
	/// @brief：		停止按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_btn_stop_();

	/// ----------------------------------------------------------------------------
	/// @brief：		校验和类型下拉框选择
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_combo_sum_type_cur_index_has_changed_(const QString &text);

	/// ----------------------------------------------------------------------------
	/// @brief：		发送文件按钮点击
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_send_cb_send_file_clicked_(bool clicked);

	/// ----------------------------------------------------------------------------
	/// @brief：		发送一次按钮点击
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_send_push_btn_send_once_();


private slots:

	/// ----------------------------------------------------------------------------
	/// @brief：		接收中，点击了保存文件按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_cb_recv_save_file_clicked_();	

	/// ----------------------------------------------------------------------------
	/// @brief：		选择接收保存文件路径
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_recv_select_path_();


	/// ----------------------------------------------------------------------------
	/// @brief：		打开文件夹按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_recv_open_folder_();

	/// ----------------------------------------------------------------------------
	/// @brief：		过滤重置按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_filter_reset_();

	/// ----------------------------------------------------------------------------
	/// @brief：		过滤check按钮的clicked
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_cb_recv_filter_clicked_(bool clicked);

	/// ----------------------------------------------------------------------------
	/// @brief：		重置接收字节数
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_recv_clear_recv_count_();

	/// ----------------------------------------------------------------------------
	/// @brief：		清空接收数据
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_recv_clear_recv_data_();

	/// ----------------------------------------------------------------------------
	/// @brief：		接收刷新按钮
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_cb_recv_refresh_clicked_(bool checked );


	/// ----------------------------------------------------------------------------
	/// @brief：	发送数据输入框内容变化槽函数
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_send_data_line_edit_data_changed_(const QString &text);

	/// ----------------------------------------------------------------------------
	/// @brief：		分隔符发生变化
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_send_split_char_line_edit_data_changed_(const QString& text);

	/// ----------------------------------------------------------------------------
	/// @brief：		接收过滤起始索引发生变化
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_le_recv_filter_index_changed_(const QString& text);

	/// ----------------------------------------------------------------------------
	/// @brief：		过滤的字节数是多少
	/// @param: 	const QString & text - text
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_combobox_recv_filter_length_changed_(const QString& text);




	/// ----------------------------------------------------------------------------
	/// @brief：		过滤按钮点击
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_push_btn_recv_filter_clicked_();


	/// ----------------------------------------------------------------------------
	/// @brief：		接收保存方式发生变化`
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_combobox_recv_save_type_current_has_changed_(const QString& text);
	

	/// ----------------------------------------------------------------------------
	/// @brief：		接收中的 set按钮
	/// @param: 	bool clicked - clicked
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void slot_cb_recv_frame_config_set_clicked_(bool clicked);

private:

	/// ----------------------------------------------------------------------------
	/// @brief：		构造函数中调用
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_ui_();


	/// ----------------------------------------------------------------------------
	/// @brief：		初始化接收
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_recv_();

	/// ----------------------------------------------------------------------------
	/// @brief：		设置发送区域控件是否启用
	/// @param: 	const bool is_enabled - is_enabled
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void set_send_config_ctrls_enable_(const sa_def::send_input_ctrls_enable_config& config);
	void set_send_config_ctrls_enable_(const sa_def::send_func_ctrl_enable_config& config);

	/// ----------------------------------------------------------------------------
	/// @brief：		发送初始化控件是否启用
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_send_ctrls_enable_();

	/// ----------------------------------------------------------------------------
	/// @brief：		绑定信号槽
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_sigs_and_slots_();


	/// ----------------------------------------------------------------------------
	/// @brief：		初始化UI控件的默认值和设置属性
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	void init_ui_ctrls_properties_();

	/// ----------------------------------------------------------------------------
	/// @brief：		发送输入检查
	/// @return: 	void - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int send_input_check_(sa_def::send_data_config& out_data_config);

	/// ----------------------------------------------------------------------------
	/// @brief：		检查输入不计算校验和的输入是否合法
	/// @param: 	const QString & str_except_sum - str_except_sum
	/// @return: 	int - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int check_except_sum_pos_(QString& str_except_sum);
	

	/// ----------------------------------------------------------------------------
	/// @brief：		得到输入的不计算校验和位置
	/// @param: 	const QString & str_except_sum - str_except_sum
	/// @return: 	int - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int get_except_sum_pos_(const QString& str_except_sum, QList<int>& out_list_except);

	/// ----------------------------------------------------------------------------
	/// @brief：		检查分隔符是否正确
	/// @param: 	const QString & str_split - str_split
	/// @return: 	int - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int check_split_char_(const QString& str_split);

	/// ----------------------------------------------------------------------------
	/// @brief：		获取分隔符
	/// @param: 	QString str_split - str_split
	/// @param: 	QList<QChar> & out_list - out_list
	/// @return: 	int - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int get_split_char_(QString str_split,	QList<QChar>& out_list);



	/// ----------------------------------------------------------------------------
	/// @brief：		接收过滤数据变化
	/// @param: 	const QString & text - text
	/// @return: 	bool - 
	///				true - 成功
	///				false - 失败
	/// 			
	/// ----------------------------------------------------------------------------
	bool check_le_recv_filter_data_(const QString& text);


private:
	/// ----------------------------------------------------------------------------
	/// @brief：		读取控件输入
	/// @param: 	QLineEdit * ple - ple
	/// @return: 	QT_NAMESPACE::QString - 无
	/// 			
	/// ----------------------------------------------------------------------------
	QString get_le_text_(QLineEdit* ple);		

	/// ----------------------------------------------------------------------------
	/// @brief：		读取校验和方式
	/// @param: 	const QString & str_type - str_type
	/// @return: 	int - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int get_sum_type_(const QString& str_type);

	/// ----------------------------------------------------------------------------
	/// @brief：		将str_send_data按照list_split_char进行切割，并将分割后的字符串返回
	/// @param: 	QString & str_send_data - str_send_data
	/// @param: 	const QList<QChar> & list_split_char - list_split_char
	/// @return: 	QT_NAMESPACE::QString - 无
	/// 			
	/// ----------------------------------------------------------------------------
	int show_send_data_( QString str_send_data, const QList<QChar>& list_split_char);


private:
	/// ----------------------------------------------------------------------------
	Ui::link_config_widget *ui = nullptr;

	/// 发送数据分隔符
	QList<QChar> list_split_char_;

	/// 发送数据的配置
	sa_def::send_data_config send_data_config_;

	/// 下面的是接收数据的配置
	sa_def::recv_data_config recv_data_config_;

	/// 保存文件类型
	QMap<QString, int >		map_save_file_type_;

	/// 成功发送字节统计
	qint64					send_total_bytes_	 = 0;

	/// 发送数据类型
	QMap<QString, int>		map_send_data_type_;

	/// 当前通道类别
	sa_def::enProtocalType	cur_channel_type_;

	/// 串口接收数据配置
	sa_def::sp_recv_frame_info sp_recv_frame_info_;
};


#endif /// link_config_widget_h